package com.ruoyi.project.mahjong.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.enu.SourceState;
import com.ruoyi.project.mahjong.domain.vo.Report;
import com.ruoyi.project.mahjong.domain.vo.ReportVo;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.DeskMemberMapper;
import com.ruoyi.project.mahjong.domain.DeskMember;
import com.ruoyi.project.mahjong.service.IDeskMemberService;

/**
 * 成桌信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-26
 */
@Service
public class DeskMemberServiceImpl implements IDeskMemberService
{
    @Autowired
    private DeskMemberMapper deskMemberMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 查询成桌信息
     *
     * @param id 成桌信息ID
     * @return 成桌信息
     */
    @Override
    public DeskMember selectDeskMemberById(Long id)
    {
        return deskMemberMapper.selectDeskMemberById(id);
    }

    /**
     * 查询成桌信息列表
     *
     * @param deskMember 成桌信息
     * @return 成桌信息
     */
    @Override
    public List<DeskMember> selectDeskMemberList(DeskMember deskMember)
    {
        return deskMemberMapper.selectDeskMemberList(deskMember);
    }

    /**
     * 新增成桌信息
     *
     * @param deskMember 成桌信息
     * @return 结果
     */
    @Override
    public int insertDeskMember(DeskMember deskMember)
    {
        deskMember.setCreateTime(DateUtils.getNowDate());
        return deskMemberMapper.insertDeskMember(deskMember);
    }

    /**
     * 修改成桌信息
     *
     * @param deskMember 成桌信息
     * @return 结果
     */
    @Override
    public int updateDeskMember(DeskMember deskMember)
    {
        deskMember.setUpdateTime(DateUtils.getNowDate());
        return deskMemberMapper.updateDeskMember(deskMember);
    }

    /**
     * 批量删除成桌信息
     *
     * @param ids 需要删除的成桌信息ID
     * @return 结果
     */
    @Override
    public int deleteDeskMemberByIds(Long[] ids)
    {
        return deskMemberMapper.deleteDeskMemberByIds(ids);
    }

    /**
     * 删除成桌信息信息
     *
     * @param id 成桌信息ID
     * @return 结果
     */
    @Override
    public int deleteDeskMemberById(Long id)
    {
        return deskMemberMapper.deleteDeskMemberById(id);
    }

    @Override
    public List<ReportVo> reportlist(DeskMember deskMember) {
        List<Report>   reportList= deskMemberMapper.reportlist(deskMember);
        List<ReportVo> reportVoList=new ArrayList<>();
        for(Report report:reportList) {
            if (ObjectUtil.isNotNull(report)) {
                Long userId = report.getUserId();
                ReportVo vo = getVo(reportVoList, userId, report.getUserName());
                System.out.println(vo);
                System.out.println(report.getSource());
                if (SourceState.YQ.getValue().equals(report.getSource())) {
                    vo.setYqCount(report.getCount());
                } else if (SourceState.ZY.getValue().equals(report.getSource())) {
                    vo.setZyCount(report.getCount());
                } else {
                    vo.setOtherCount(report.getCount());
                }
                setVo(reportVoList, vo);
            }
        }
        return reportVoList;
    }

    private void setVo(List<ReportVo> reportVoList, ReportVo vo) {
        if(ObjectUtil.isEmpty(reportVoList)){
              reportVoList.add(vo);
        }
        int flag=0;
        for(ReportVo reportVo:reportVoList ){
           if(ObjectUtil.isNotNull(vo.getUserId())&&ObjectUtil.isNotNull(reportVo.getUserId())) {
               if (vo.getUserId().equals(reportVo.getUserId())) {
                   flag = 1;
                   break;
               }
           }
        }
        if(flag==0){
            reportVoList.add(vo);
        }
    }

    private ReportVo getVo(List<ReportVo> reportVoList, Long userId,String userName) {
        ReportVo reportVo=     new ReportVo();
        reportVo.setUserId(userId);
        reportVo.setUserName(userName);
        if(ObjectUtil.isEmpty(reportVoList)){
            return  reportVo;
        }
        for(ReportVo vo:reportVoList ){
            if(ObjectUtil.isNotNull(vo.getUserId())&&ObjectUtil.isNotNull(userId)) {
                if (vo.getUserId().equals(userId)) {
                    vo.setUserName(userName);
                    return vo;
                }
            }
        }
        return reportVo;
    }
}
